package com.bocai.yuweiwangluo.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.fragment.Fragment_Home;
import com.bocai.yuweiwangluo.fragment.Fragment_Me;
import com.bocai.yuweiwangluo.fragment.Fragment_Talk_new;
import com.lidroid.xutils.ViewUtils;

/**
 * 主页面
 */
public class HomeActivity extends BaseActivity implements View.OnClickListener
{
    private LinearLayout mTabRenWu;
    private LinearLayout mTabQianBao;
    private LinearLayout mTabUser;

    private ImageButton mRenwuImg;
    private ImageButton mQianBaoImg;
    private ImageButton mUserImg;

    private TextView mRenWutxt;
    private TextView mQianBaotxt;
    private TextView mUsertxt;

    private Fragment mTab01;
    private Fragment mTab02;
    private Fragment mTab03;


    @Override
    protected int contentView()
    {
        return R.layout.activity_home;
    }

    @Override
    protected void setData()
    {
        ViewUtils.inject(this);
        initView();
        initEvent();

    }
    protected void initEvent()
    {
        mTabRenWu.setOnClickListener(this);
        mTabQianBao.setOnClickListener(this);
        mTabUser.setOnClickListener(this);
    }


    private void initView()
    {
        mRenwuImg = (ImageButton) findViewById(R.id.id_tab_renwu_img);
        mQianBaoImg = (ImageButton) findViewById(R.id.id_tab_qianbao_img);
        mUserImg = (ImageButton) findViewById(R.id.id_tab_user_img);

        mRenWutxt = (TextView) findViewById(R.id.id_tab_renwu_txt);
        mQianBaotxt = (TextView) findViewById(R.id.id_tab_qianbao_txt);
        mUsertxt = (TextView) findViewById(R.id.id_tab_user_txt);

        mTabRenWu = (LinearLayout) findViewById(R.id.id_tab_renwu);
        mTabQianBao = (LinearLayout) findViewById(R.id.id_tab_qianbao);
        mTabUser = (LinearLayout) findViewById(R.id.id_tab_user);
        setSelect(0);
    }

    public void setSelect(int i)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i)
        {
            case 0:
                if (mTab01 == null)
                {
                    mTab01 = new Fragment_Home();
                    transaction.add(R.id.id_content, mTab01);
                } else
                {
                    transaction.show(mTab01);
                }
                mRenwuImg.setImageResource(R.mipmap.home_bottom_home2);
                break;
            case 1:
                if (mTab02 == null)
                {
                    mTab02 = new Fragment_Talk_new();
                    transaction.add(R.id.id_content, mTab02);
                } else
                {
                    transaction.show(mTab02);
                }
                mQianBaoImg.setImageResource(R.mipmap.home_bottom_talk2);
                break;
            case 2:
                if (mTab03 == null)
                {
                    mTab03 = new Fragment_Me();
                    transaction.add(R.id.id_content, mTab03);
                } else
                {
                    transaction.show(mTab03);
                }
                mUserImg.setImageResource(R.mipmap.home_bottom_me2);
                break;
        }
        transaction.commit();
    }

    private void resetImg()
    {
        mRenwuImg.setImageResource(R.mipmap.home_bottom_home1);
        mQianBaoImg.setImageResource(R.mipmap.home_bottom_talk1);
        mUserImg.setImageResource(R.mipmap.home_bottom_me1);



    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
        if (mTab03 != null)
        {
            transaction.hide(mTab03);
        }


    }

    @Override
    public void onClick(View v)
    {
        resetImg();
        switch (v.getId())
        {
            case R.id.id_tab_renwu:
                setSelect(0);
                break;
            case R.id.id_tab_qianbao:
                setSelect(1);
                break;
            case R.id.id_tab_user:
                setSelect(2);
                break;
        }
    }
}
